"""
@name : 三乐购物系统
@author : zhangjian
@projectname: tlbb
"""

userinfo={"zj":"8"}
goodsinfo={'F00001': {'name':'苹果','price':1.2},'F00002': {'name':'香蕉','price':5.5}}
bought={}
budget=0

def login_interface():
    print("欢迎来到三乐购物系统，输入用户名和密码进入系统：")
    username = input("输入用户名：")
    password = input("输入密码：")
    if signin_check(username,password):
        print("登录成功")
        global budget
        budget=float(input("输入购物预算："))
        return True
    else:
        print("用户名或密码输入错误，登录失败，请重新输入：")
        return False

def signin_check(username,password):
    if username in userinfo.keys() and userinfo.get(username,False)==password:
        return True
    else:
        return False

def print_goodsinfo():
    print("商品信息如下：")
    for key in goodsinfo.keys():
        print(key,end='：')
        for j in (goodsinfo.get(key,False)).items():
            print(j,end='')
        print()

def shopping(budget):
    while True:
        buy=input("输入商品编号以购买相应的商品(输入1查看已购买商品，输入exit退出)：")
        if buy=='1':
            print_bought()
        elif buy=="exit":
            print_bought()
            print(f"余额：{budget}")
            return True
        else:
            if (goodsinfo.get(buy,False))["price"]<=budget:
                budget-=(goodsinfo.get(buy, False))["price"]
                bought[buy]=goodsinfo.get(buy,False)
                print("购买成功")
            else:
                print("余额不足，购买失败")

def print_bought():
    if len(bought)==0:
        print("你还没有购买任何商品")
    else:
        print("已购买的商品有：")
        for key in bought.keys():
            print(key, end='：')
            for j in (bought.get(key, False)).items():
                print(j, end='')
            print()

while True:
    check=login_interface()
    if check:
        print_goodsinfo()
        exit=shopping(budget)
        if exit:
            break
